package action;




import java.util.ArrayList;
import java.util.Map;


import models.*;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;



public class PreUpdateAuthenticationAction extends ActionSupport {
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String submit = "";
	

	public String getSubmit() {
		return submit;
	}

	public void setSubmit(String submit) {
		this.submit = submit;
	}
	private int IssueNumber=0;
	private String secretQuestion="";
    private String answer="";
    Map<String, Object> session;
    
	public int getIssueNumber() {
		return IssueNumber;
	}

	public void setIssueNumber(int IssueNumber) {
		this.IssueNumber = IssueNumber;
	}
	
	

	public String getSecretQuestion() {
		return secretQuestion;
	}

	public void setSecretQuestion(String secretQuestion) {
		this.secretQuestion = secretQuestion;
	}

	public String getAnswer() {
		return answer;
	}

	public void setAnswer(String answer) {
		this.answer = answer;
	}
	
    

	public String execute() {
		 ArrayList<TagData> Tags = new ArrayList<TagData>();

        //session.put("issuenum",IssueNumber);
        System.out.println("issue number="+IssueNumber);
       // ArrayList<IssueData> Issues=new ArrayList<IssueData>();
        if (this.IssueNumber==0) {
        	addActionError(getText("Issue Number can not be Empty !!"));
        	System.out.println("empty");
		    return "error";
		    
        } 
        if (this.secretQuestion.isEmpty()||this.answer.isEmpty()) {
        	addActionError(getText("Please give the Secret Question/Answer pair !!"));
        	System.out.println("empty");
		    return "error";
        } 
        IssueData Issue=IssueData.findOne("where issue_num = "+this.IssueNumber);
        if(Issue==null)
        {
        		addActionError(getText("No Issue is registered with the specified Issue Number"));
        		return "error";
        }
        
        Tags = TagData.Tag_name("where issue_id="+Issue.getIssue_id());
        String tags_with_commas = "";
        for(int i=0;i<Tags.size();i++){
			tags_with_commas=Tags.get(i).getTagName()+","+tags_with_commas;
        }
        System.out.println("tags are"+tags_with_commas);
        Issue.setTags(tags_with_commas);
        System.out.println(Issue.getIssue_num()+","+Issue.getSeq_qn()+","+Issue.getSeq_ans());
        System.out.println(this.IssueNumber+","+this.secretQuestion+","+this.answer);
        if (this.IssueNumber==Issue.getIssue_num()
        		&&this.answer.equals(Issue.getSeq_ans()) ){
			System.out.println("issue subject is"+Issue.getIssue_subject());
        	session = ActionContext.getContext().getSession();
        	session.put("Issue",Issue);
        	
        	            return "success";
        } 
		addActionError(getText("The Security question and/or Answer doesn't match !! Try Again !!"));
        return "error";
        }
        
	}
